package horseParkMonitor.components.table;

import horseParkMonitor.Terminal;
import horseParkMonitor.components.Footer;
import horseParkMonitor.components.Header;
import horseParkMonitor.components.MainScreen;
import horseParkMonitor.configuration.*;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;


import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class GridTable {
    public static TableView<Horse> tableViewHorses;
    public static TableView<Bill> tableViewInventory;
    public static TableView<Bill> tableViewDispension;
    private HBox container;
    HBox separator,separator1, separator2, separator3, separator4, separator5;
    ObservableList<Horse> listhorses;
    ObservableList<Bill> inventory;
    ObservableList<Bill> dispension;
    public static List<Horse> horses=new ArrayList<Horse>();
    public static List<Bill> billsInventory=new ArrayList<Bill>();
    public static List<Bill> billsDispension=new ArrayList<Bill>();
    public static Bill billDispension=new Bill();

    public GridTable() {

      container=new HBox();

      container.setMaxSize(MainScreen.width, MainScreen.height-Header.heightHeader-Footer.heightFooter-150);
      container.setMinSize(MainScreen.width, MainScreen.height-Header.heightHeader-Footer.heightFooter-150);
      container.setAlignment(Pos.TOP_LEFT);

      separator=new HBox();separator.setMinSize(MainScreen.width,25);
      separator1=new HBox();separator1.setMinSize(MainScreen.width,30);
        separator2=new HBox();separator2.setMinSize(MainScreen.width,25);
        separator3=new HBox();separator3.setMinSize(MainScreen.width,30);
        separator4=new HBox();separator4.setMinSize(MainScreen.width,25);
        separator5=new HBox();separator5.setMinSize(MainScreen.width,30);

        final VBox part1=new VBox();
        part1.setMaxSize(MainScreen.width-400,MainScreen.height-Header.heightHeader-Footer.heightFooter-150);
        part1.setMinSize(MainScreen.width-400, MainScreen.height - Header.heightHeader - Footer.heightFooter-150);
        try {
            horses = HorseList.unmarshal(new File(HorseList.configurationFileName));
        }catch(Throwable ex){
            System.out.println(ex.getMessage());
            Terminal.getMainScreen().showTerminalError("Can't read dataBase!");
        }
         listhorses= FXCollections.observableArrayList();
        if(horses!=null) {
            for (Horse horse : horses) {
                listhorses.add(horse);
            }
        }

        buildTableHorse(part1);
        container.getChildren().add(part1);


        final VBox part2=new VBox();
        part2.setMaxSize(200,MainScreen.height-Header.heightHeader-Footer.heightFooter-150);
        part2.setMinSize(200, MainScreen.height - Header.heightHeader - Footer.heightFooter-150);
        try {
            billsInventory = Inventory.unmarshal(new File(Inventory.configurationFileName));
        }catch(Throwable ex){
            System.out.println(ex.getMessage());
            Terminal.getMainScreen().showTerminalError("Can't read dataBase!");
        }
        inventory= FXCollections.observableArrayList();
        if(billsInventory!=null) {
            for (Bill bill : billsInventory) {
                inventory.add(bill);
            }
        }

        buildTableInventory(part2);
        container.getChildren().add(part2);

        final VBox part3=new VBox();
        part3.setMaxSize(200,MainScreen.height-Header.heightHeader-Footer.heightFooter-150);
        part3.setMinSize(200, MainScreen.height - Header.heightHeader - Footer.heightFooter-150);
        try {
            billsDispension = Dispensing.unmarshal(new File(Dispensing.configurationFileName));
        }catch(Throwable ex){
            System.out.println(ex.getMessage());
            Terminal.getMainScreen().showTerminalError("Can't read dataBase!");
        }
        dispension= FXCollections.observableArrayList();
        if(billsDispension!=null) {
            for (Bill bill : billsDispension) {
                dispension.add(bill);
            }
        }

        buildTableDispensing(part3);
        container.getChildren().add(part3);

    }
    private void buildTableDispensing(VBox partDisplay){
        partDisplay.getChildren().clear();
        partDisplay.getChildren().add(separator4);
        Label name=new Label("Dispension");
        name.setStyle("-fx-background-color: brown;-fx-font-size: 25");
        partDisplay.getChildren().add(name);

        partDisplay.setAlignment(Pos.CENTER_LEFT);
        tableViewDispension=new TableView<Bill>(dispension);
        tableViewDispension.setTableMenuButtonVisible(true);
        tableViewDispension.setCursor(Cursor.TEXT);
        tableViewDispension.setPrefHeight(MainScreen.height- Header.heightHeader- Footer.heightFooter-150);
        tableViewDispension.setMaxHeight(MainScreen.height - Header.heightHeader - Footer.heightFooter - 150);;
        tableViewDispension.setPrefWidth(200);
        tableViewDispension.setMaxWidth(200);
        tableViewDispension.setTooltip(new Tooltip("dispension table"));
        tableViewDispension.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
        tableViewDispension.setStyle("-fx-background-color: burlywood; -fx-padding: 10; -fx-font-size: 16;-fx-font-weight: bold;");
        tableViewDispension.setEditable(true);

        TableColumn nameColumn1=new TableColumn("Denomination");
        nameColumn1.setCellValueFactory(new PropertyValueFactory<Horse, String>("denomination"));

        nameColumn1.setSortable(true);
        nameColumn1.setPrefWidth(200/2);
        nameColumn1.setResizable(false);
        nameColumn1.setEditable(true);

        TableColumn nameColumn2=new TableColumn("Count");
        nameColumn2.setCellValueFactory(new PropertyValueFactory<Horse, String>("count"));

        nameColumn2.setSortable(true);
        nameColumn2.setPrefWidth(200/2);
        nameColumn2.setResizable(false);
        nameColumn2.setEditable(true);
        tableViewDispension.setItems(dispension);
        tableViewDispension.getColumns().addAll(nameColumn1,nameColumn2);

        partDisplay.getChildren().add(tableViewDispension);
        partDisplay.getChildren().add(separator5);

    }

    private void buildTableInventory(VBox partDisplay){
        partDisplay.getChildren().clear();
        partDisplay.getChildren().add(separator2);
        Label name=new Label("Inventory");
        name.setStyle("-fx-background-color: brown;-fx-font-size: 25");
        partDisplay.getChildren().add(name);

        partDisplay.setAlignment(Pos.CENTER_LEFT);
        tableViewInventory=new TableView<Bill>(inventory);
        tableViewInventory.setTableMenuButtonVisible(true);
        tableViewInventory.setCursor(Cursor.TEXT);
        tableViewInventory.setPrefHeight(MainScreen.height- Header.heightHeader- Footer.heightFooter-150);
        tableViewInventory.setMaxHeight(MainScreen.height - Header.heightHeader - Footer.heightFooter - 150);;
        tableViewInventory.setPrefWidth(200);
        tableViewInventory.setMaxWidth(200);
        tableViewInventory.setTooltip(new Tooltip("inventory table"));
        tableViewInventory.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
        tableViewInventory.setStyle("-fx-background-color: olivedrab; -fx-padding: 10; -fx-font-size: 16;-fx-font-weight: bold;");
        tableViewInventory.setEditable(true);

        TableColumn nameColumn1=new TableColumn("Denomination");
        nameColumn1.setCellValueFactory(new PropertyValueFactory<Horse, String>("denomination"));

        nameColumn1.setSortable(true);
        nameColumn1.setPrefWidth(200/2);
        nameColumn1.setResizable(false);
        nameColumn1.setEditable(true);

        TableColumn nameColumn2=new TableColumn("Count");
        nameColumn2.setCellValueFactory(new PropertyValueFactory<Horse, String>("count"));

        nameColumn2.setSortable(true);
        nameColumn2.setPrefWidth(200/2);
        nameColumn2.setResizable(false);
        nameColumn2.setEditable(true);
        tableViewInventory.setItems(inventory);
        tableViewInventory.getColumns().addAll(nameColumn1,nameColumn2);

        partDisplay.getChildren().add(tableViewInventory);
        partDisplay.getChildren().add(separator3);

    }
   private void buildTableHorse(VBox partDisplay){

       partDisplay.getChildren().clear();

       partDisplay.getChildren().add(separator);
       Label name=new Label("Horses");
       name.setStyle("-fx-background-color: brown;-fx-font-size: 25");
       partDisplay.getChildren().add(name);
       partDisplay.setAlignment(Pos.CENTER_LEFT);

       tableViewHorses=new TableView<Horse>(listhorses);
       tableViewHorses.setTableMenuButtonVisible(true);
       tableViewHorses.setCursor(Cursor.TEXT);
       tableViewHorses.setPrefHeight(MainScreen.height- Header.heightHeader- Footer.heightFooter-150);
       tableViewHorses.setMaxHeight(MainScreen.height - Header.heightHeader - Footer.heightFooter - 150);;
       tableViewHorses.setPrefWidth(MainScreen.width-400);
       tableViewHorses.setMaxWidth(MainScreen.width-400 );
       tableViewHorses.setTooltip(new Tooltip("horses table"));
       tableViewHorses.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
       tableViewHorses.setStyle("-fx-background-color: yellow; -fx-padding: 10; -fx-font-size: 16;-fx-font-weight: bold;");
       tableViewHorses.setEditable(true);

       TableColumn nameColumn1=new TableColumn("Number");
       nameColumn1.setCellValueFactory(new PropertyValueFactory<Horse, String>("horse_number"));

       nameColumn1.setSortable(true);
       nameColumn1.setPrefWidth(80);
       nameColumn1.setResizable(false);
       nameColumn1.setEditable(true);

       TableColumn nameColumn2=new TableColumn("Name");
       nameColumn2.setCellValueFactory(new PropertyValueFactory<Horse,String>("name_horse"));
       nameColumn2.setSortable(true);
       nameColumn2.setPrefWidth((MainScreen.width-400) -240);
       nameColumn2.setResizable(false);
       nameColumn2.setEditable(true);

       TableColumn nameColumn3=new TableColumn("Odds");
       nameColumn3.setCellValueFactory(new PropertyValueFactory<Horse,String>("odds"));
       nameColumn3.setSortable(true);
       nameColumn3.setPrefWidth(80);
       nameColumn3.setResizable(false);
       nameColumn3.setEditable(true);

       TableColumn nameColumn4=new TableColumn("Did-win");
       nameColumn4.setCellValueFactory(new PropertyValueFactory<Horse,String>("did_win"));
       nameColumn4.setSortable(true);
       nameColumn4.setPrefWidth(80);
       nameColumn4.setResizable(false);
       nameColumn4.setEditable(true);
       nameColumn4.setCellFactory(TextFieldTableCell.<ObservableList<StringProperty>>forTableColumn());  //

       tableViewHorses.setItems(listhorses);
       tableViewHorses.getColumns().addAll(nameColumn1,nameColumn2,nameColumn3,nameColumn4);

       partDisplay.getChildren().add(tableViewHorses);
       partDisplay.getChildren().add(separator1);



   }
public static boolean findBillInDispension(String denomination){
   for(Bill bill:billsDispension){
      if(bill.getDenomination().equals(denomination+"$")){ billDispension=bill;return true;}

   }
   return false;
}
public static String getWin(){
    int summaWin=0;
    for(Bill bill:billsDispension){
        summaWin+=(Integer.valueOf(bill.denomination.substring(0, bill.denomination.length()-1))*Integer.valueOf(bill.count));

    }
  return String.valueOf(summaWin);
}
    public static String getRealSumma(){
        int summaReal=0;
        for(Bill bill:billsInventory){
            summaReal+=(Integer.valueOf(bill.denomination.substring(0, bill.denomination.length()-1))*Integer.valueOf(bill.count));

        }
        return String.valueOf(summaReal);
    }

    public Node show() {
        return container;
    }

    public static TableView<Horse> getTableViewHorses() {
        return tableViewHorses;
    }
}
